//
//  DishManager.m
//  takeout
//
//  Created by Derekc on 12-3-1.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DishManager.h"
#import "ResolveXML.h"
#import "Constants.h"
#import "DishCategory.h"
#import "Dish.h"

@implementation DishManager

-(NSArray *)getAllDishesBySupId:(NSString *)supplierId{
    ResolveXML *resolveXML=[[ResolveXML alloc]init];
    NSString *URIString = [NSString stringWithFormat:SERVICE_DOMAIN, [NSString stringWithFormat: SERVICE_ALL_DISHES_BY_SUPPLIER,supplierId]];
    CUSTOM_LOG(URIString);
    GDataXMLDocument *doc=[resolveXML getXMLDocument:URIString];
    NSArray *dishCategories = [doc.rootElement children]; 
    NSMutableArray *result = nil;
    for (GDataXMLElement *element in dishCategories) {
        if ([element isKindOfClass:[GDataXMLElement class]]) {
            DishCategory *dishCategory = [[DishCategory alloc] init];
            dishCategory.dishCategoryId = [[[element childAtIndex:0] stringValue] integerValue];
            dishCategory.dishCategoryName = [[element childAtIndex:1] stringValue];
            GDataXMLElement *dishElements = (GDataXMLElement *)[element childAtIndex:2];
            NSArray *dishArray = [dishElements children];
            if ([dishArray count] > 0) {
                NSMutableArray *dishes = nil;
                for (GDataXMLElement *dishElement in dishArray) {
                    if ([dishElement isKindOfClass:[GDataXMLElement class]]) {
                        Dish *dish = [[Dish alloc] init];
                        dish.dishId = [[[dishElement childAtIndex:0] stringValue] integerValue];
                        dish.dishName = [[dishElement childAtIndex:1] stringValue];
                        dish.imgUrl = [[dishElement childAtIndex:2] stringValue];
                        dish.desc = [[dishElement childAtIndex:3] stringValue];
                        dish.price = [[[dishElement childAtIndex:4] stringValue] floatValue];
                        dish.secondPrice = [[[dishElement childAtIndex:5] stringValue] floatValue];
                        dish.thirdPrice = [[[dishElement childAtIndex:6] stringValue] floatValue];
                        dish.dishCategoryId =  [[[dishElement childAtIndex:7] stringValue] integerValue];
                        
                        if (dishes == nil) {
                            dishes = [NSMutableArray arrayWithObject:dish];
                        }else{
                            [dishes addObject:dish];
                        }
                        [dish release];
                    }
                }
                dishCategory.dishes = dishes;
                if (result == nil) {
                    result = [NSMutableArray arrayWithObject:dishCategory];
                }else{
                    [result addObject:dishCategory];
                }
                
                [dishCategory release];
            }
        }
    }
    [resolveXML release];
    return result;
}

@end
